package mar.bt.impl.ui;

import java.io.IOException;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;

import org.mobileandembedded.idesktop.ClientSideHandler;

import com.mar.meclient.bt.BTMidlet;
import com.mar.meclient.bt.IBluetoothIOHandler;
import com.mar.meclient.util.ui.IPage;
import com.mar.meclient.util.ui.IPageNavigator;

public class RemoteBTMainPage implements IPage {
	private final IPageNavigator pageNavigator;
	
	private ClientSideHandler clientSideHandler;
	
	private Displayable page;
	
	public RemoteBTMainPage(IPageNavigator navigator) {
		this.pageNavigator = navigator;
	}
	
	public void createPageContent(Object modelInput) {
		try {
			final IBluetoothIOHandler btIoHandler = (IBluetoothIOHandler)modelInput;
			clientSideHandler = new ClientSideHandler( btIoHandler );
			page = clientSideHandler.getRemoteDesktopCanvas();
			page.setTitle("Hadler started");
			
			new Thread(new Runnable(){
				public void run() {
						try {
							Thread.sleep(1000);
					        clientSideHandler.sendDeviceConfigurationMessage();
					        clientSideHandler.sendRequestScreenshotMessage();
					        Display.getDisplay( BTMidlet.getInstance() ).getCurrent().setTitle("Configuration is sent");
						} catch (Exception e) {
							getDesplayable().setTitle("!>> "+e.toString());
						}
			}}).start();

		} catch (IOException e) {
			BTMidlet.getInstance().showAlert(e);
		}
	}

	public Displayable getDesplayable() {
		return page;
	}

}
